#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;
using TVC.Layout;
using System.Xml;
using System.IO;
using BUS;
using DTO;
using DAO;
using System.Text.RegularExpressions;
#endregion

namespace TVC.Modules.Ref
{
    public partial class BUD : ModuleParent, IHostedApplication
    {
        //int idAllowEditLanguage = 1;
        int idEdit = 223;
        string strAction = "";

        GridLayout layout = null;

        public BUD()
        {
            InitializeComponent();
            _strType = "BUD";

        }
        public void InitializeApplication()
        {

        }

        //public void AddNew()
        //{

        //}

        public void SetObject(object obj)
        {
        }
        private ToolBar GetTbarLocal()
        {
            if (this.Parent.Controls["mobjToolBarMain"] is ToolBar) return this.Parent.Controls["mobjToolBarMain"] as ToolBar;
            if (this.Parent.Parent.Controls["mobjToolBarMain"] is ToolBar) return this.Parent.Controls["mobjToolBarMain"] as ToolBar;

            return null;
        }
        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            if (IsPermis(idEdit)) objElements.Add(new HostedToolBarButton("Edit", "Edit", new IconResourceHandle("Edit.png"), "A"));
            if (IsPermis(idEdit)) objElements.Add(new HostedToolBarButton("Save", "Save", new IconResourceHandle("Save.png"), "S"));
            objElements.Add(new HostedToolBarButton("Skip", "Skip", new IconResourceHandle("Reject.png"), "K"));


            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            Cancel = false;
            try
            {
                sErr = "";
                string strAction = (string)objButton.Tag;

                switch (strAction)
                {
                    case "A":
                        Edit();
                        break;
                    case "S":
                        saveRecord(nPeriodFrom);
                        break;
                    case "K":
                        CancelEdit();
                        break;
                }

                if (!Cancel)
                {
                    SetupToolBar(strAction, _processStatus);
                    _processStatus = strAction;

                }
                if (sErr == "")
                    ThrowMsg("Command: " + _processStatus);

            }
            catch (Exception ex)
            {
                ThrowMsg(ex.Message + Environment.NewLine + ex.Source);
            }
            sErr = "";
            Cancel = false;
        }

        private void CancelEdit()
        {

        }



        private void Edit()
        {
            EnablePrimary(true);
        }




        public override void saveRecord(Control text, bool isDraft = false)
        {
            BUS.BUDControl busCtr = new BUDControl();

            DTO.GlobalParametersInfo info = GetData();

            sErr = busCtr.insertUpdate(info);
            if (sErr == "")
            {
                //RefreshContract();
                MessageBox.Show("Your record is successfull saved!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information, false);
                EnablePrimary(false);
            }
            else
            {
                Cancel = true;
                MessageBox.Show(sErr, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, false);
            }

            ThrowMsg(sErr);

            //Rebind();
        }


        private bool ValidateForm(ref string msg)
        {
            string sErr = "", sWarn = "";
            foreach (Control x in this.Controls)
            {
                if (errProvider.GetError(x) != string.Empty) sErr += string.Format("Error: {0}\n", errProvider.GetError(x));
                //if (warningProvider.GetError(x) != string.Empty) sWarn += string.Format("Warning: {0}\n", warningProvider.GetError(x));
            }

            msg = string.Format("{0}{1}", sErr, sWarn);

            return sErr == "";

        }




        private void InvoiceRegister_Load(object sender, EventArgs e)
        {
            GetMainForm();
            BUS.BUDControl ctr = new BUDControl();
            string sErr = "";
            SetData(ctr.get(ref sErr));
            EnablePrimary(false);
        }





        private void SetData(GlobalParametersInfo info)
        {
            if (info != null)
            {
                nPeriodFrom.Value = info.PeriodFrom;
                nPeriodTo.Value = info.PeriodTo;
                nTolerance.Value = (Decimal)info.Tolerance;
            }
            else
            {
                nPeriodFrom.Value = DateTime.Today.Year * 1000 + 1;
                nPeriodTo.Value = DateTime.Today.Year * 1000 + 12;
            }
        }

        private void SetupToolBar(string strAction, string oldStatus = "V")
        {
            ToolBar tbarLocal = GetTbarLocal();
            bool on = true, off = false;
            if (tbarLocal == null) return;

            switch (strAction)
            {
                case "A":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "E") { button.Enabled = off; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "S") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = on; break; } }

                    break;
                case "S":
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "E") { button.Enabled = on; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "S") { button.Enabled = off; break; } }
                    foreach (ToolBarButton button in tbarLocal.Buttons) { if (button.Name == "K") { button.Enabled = off; break; } }

                    break;
            }
        }


        private void EnablePrimary(bool val)
        {
            foreach (Control x in this.Controls)
            {
                x.Enabled = val;
            }
        }

        private GlobalParametersInfo GetData()
        {
            GlobalParametersInfo info = new GlobalParametersInfo()
            {
                PeriodFrom = Convert.ToInt32(nPeriodFrom.Value),
                PeriodTo = Convert.ToInt32(nPeriodTo.Value),
                Tolerance = Convert.ToDouble(nTolerance.Value),

            };

            return info;

        }



    }

}